package com.github.iot.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatEditText;

import com.github.iot.R;
import com.github.iot.dao.CommonDaoUtils;
import com.github.iot.dao.DaoUtilsStore;
import com.github.iot.entity.User;
import com.github.iot.utils.BarUtils;
import com.github.iot.utils.FastJsonUtils;
import com.github.iot.utils.KeyboardUtils;
import com.github.iot.utils.SharedPreferencesUtils;
import com.github.iot.utils.ToastUtils;
import com.github.iot.view.RxTitle;
import com.github.iot.view.dialogs.fragment.SimpleDialogFragment;
import com.github.iot.view.dialogs.iface.ISimpleDialogListener;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 修改个人信息Activity
 *
 * @Author: 梁声洪
 * @Date: 2019/12/10 9:14
 * @Version: v1.0.0
 */
public class UpdateUserInfoActivity extends AppCompatActivity implements ISimpleDialogListener {

    private static final int REQUEST_SIMPLE_DIALOG = 42;

    @BindView(R.id.rx_title)
    RxTitle mTitle;

    @BindView(R.id.et_nickname)
    AppCompatEditText mNickname;

    @BindView(R.id.et_username)
    AppCompatEditText mUsername;

    @BindView(R.id.et_password)
    AppCompatEditText mPassword;

    @BindView(R.id.et_repassword)
    AppCompatEditText mRePassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BarUtils.setStatusBarColor(this, R.color.colorPrimaryDark);
        setContentView(R.layout.activity_update_user_info);

        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        mTitle.setLeftFinish(this);

        String str = SharedPreferencesUtils.getString(this, "iot_login_user", null);
        if (null != str) {
            User user = FastJsonUtils.strToBean(str, User.class);
            mNickname.setText(user.getNickname());
            mUsername.setText(user.getUsername());
        }
    }

    @OnClick({R.id.btn_update_user_info})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_update_user_info:
                SimpleDialogFragment.createBuilder(UpdateUserInfoActivity.this, getSupportFragmentManager())
                        .setTitle("系统提示")
                        .setMessage("您确定要修改个人信息吗？")
                        .setPositiveButtonText("确定")
                        .setNegativeButtonText("取消")
                        .setRequestCode(REQUEST_SIMPLE_DIALOG)
                        .setCancelable(false)
                        .show();
                break;
            default:
                break;
        }
    }

    private void updateUserInfo() {
        KeyboardUtils.hideSoftInput(UpdateUserInfoActivity.this);

        String nickname = mNickname.getText().toString();
        String username = mUsername.getText().toString();
        String password = mPassword.getText().toString();
        String repassword = mRePassword.getText().toString();

        if (TextUtils.isEmpty(nickname)) {
            ToastUtils.warning("昵称不能为空！");
            return;
        }

        if (TextUtils.isEmpty(username)) {
            ToastUtils.warning("账号不能为空！");
            return;
        }

        if (nickname.length() > 20 || nickname.length() < 3) {
            ToastUtils.warning("昵称长度格式输入有误！");
            return;
        }

        if (username.length() > 20 || username.length() < 3) {
            ToastUtils.warning("账号长度格式输入有误！");
            return;
        }

        if (!TextUtils.isEmpty(password) && !TextUtils.isEmpty(repassword)) {
            if (password.length() > 10 || password.length() < 3) {
                ToastUtils.warning("密码长度格式输入有误！");
                return;
            }

            if (!password.equals(repassword)) {
                ToastUtils.warning("两次输入密码不一致！");
                return;
            }
        }

        String str = SharedPreferencesUtils.getString(this, "iot_login_user", null);
        User user = FastJsonUtils.strToBean(str, User.class);
        user.setNickname(nickname);
        user.setUsername(username);
        user.setPassword(password);

        CommonDaoUtils<User> commonDao = DaoUtilsStore.getInstance().getUserDaoUtils();
        boolean flag = commonDao.update(user);

        if (flag) {
            SharedPreferencesUtils.putString(this, "iot_login_user", FastJsonUtils.obj2JsonStr(user));
            ToastUtils.success("个人信息修改成功");
        } else {
            ToastUtils.error("个人信息修改失败");
        }
    }

    @Override
    public void onNegativeButtonClicked(int requestCode) {

    }

    @Override
    public void onNeutralButtonClicked(int requestCode) {

    }

    @Override
    public void onPositiveButtonClicked(int requestCode) {
        if (requestCode == REQUEST_SIMPLE_DIALOG) {
            updateUserInfo();
        }
    }

}